//
//  AppDelegate.swift
//  WB
//
//  Created by macssss on 16/6/29.
//  Copyright © 2016年 macssss. All rights reserved.
//

import UIKit
import UserNotifications
import AFNetworking
 import SVProgressHUD


@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?
   
    
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        
        setAdditions()
        
        window = UIWindow()
        window?.backgroundColor = UIColor.white()
        window?.rootViewController = WBMainViewController()
        
        window?.makeKeyAndVisible()
        
        loadAppInfo()
        
        return true
        
        
    }
    
}

//MARK: -  设置应用程序额外信息
extension AppDelegate {
    
    private func  setAdditions() {
        
        SVProgressHUD.setMinimumDismissTimeInterval(1)
        
        AFNetworkActivityIndicatorManager.shared().isEnabled = true
        
        //MARK: -  用户授权
        if #available(iOS 10.0, *) {
            UNUserNotificationCenter.current().requestAuthorization(options: [.alert, .carPlay, .sound, .badge]) { (success, error) in
                
                print("授权" + (success ? "成功" : "失败"))
            }
        } else {
            
            //iOs10以前的用法
            let notifySettings = UIUserNotificationSettings(types:  [.alert, .badge, .sound], categories: nil)
            
           UIApplication.shared().registerUserNotificationSettings(notifySettings)
            
            
        }
        
    }

}

//MARK: - 从服务器加载数据
extension AppDelegate {
    
    private func loadAppInfo() {
        
            DispatchQueue.global().async {
                
                let url = Bundle.main.urlForResource("main.json", withExtension: nil)
                
                let data = NSData(contentsOf: url!)
                
                let docDir = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask , true)[0]
                
                let jsonPach = (docDir as NSString).appendingPathComponent("main.json")
                
                data?.write(toFile: jsonPach, atomically: true)
                
                print("加载完成")
                
        }
   
        
    }
    
}









